package com.heima.Service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.Serivce.WemediaCilent;
import com.heima.model.Admin.Dtos.ChannelDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.FailureCallback;
import org.springframework.util.concurrent.SuccessCallback;

import static com.heima.common.exception.RabbitMQcode.WmChannelKey;
import static com.heima.common.exception.RabbitMQcode.WmExchangeName;

@Service
@Slf4j
public class AdminChannelService implements com.heima.Service.AdminChannelService {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    WemediaCilent wemediaCilent;

//    删除频道
    @Override
    public ResponseResult ChannelDelete(Integer id) {
        CorrelationData correlationData=SetCallback(String.valueOf(id));
        String data=id+"_del";
        rabbitTemplate.convertAndSend(WmExchangeName,WmChannelKey,data,correlationData);
        return ResponseResult.okResult("Channel成功删除");
    }

//    查询频道
    @Override
    public ResponseResult GetChannelList(ChannelDto channelDto) {
        return wemediaCilent.GetChamnelList(channelDto);
    }

//     添加频道
    @Override
    public ResponseResult ChannelSave(WmChannel wmChannel) {
        if(wmChannel!=null)
        {
            CorrelationData correlationData=SetCallback(wmChannel.getName()+"_save");
            String data= JSON.toJSONString(wmChannel)+"_save";
            rabbitTemplate.convertAndSend(WmExchangeName,WmChannelKey,data,correlationData);
            return ResponseResult.okResult("Channel成功添加");
        }
        return ResponseResult.errorResult(404,"数据不能为空");
    }

//     修改频道
    @Override
    public ResponseResult ChannelUpdate(WmChannel wmChannel) {
        if(wmChannel!=null)
        {
            CorrelationData correlationData=SetCallback(wmChannel.getName()+"_update");
            String data=JSON.toJSONString(wmChannel)+"_update";
            rabbitTemplate.convertAndSend(WmExchangeName,WmChannelKey,data,correlationData);
            return ResponseResult.okResult("Channel修改成功");
        }
        return ResponseResult.errorResult(404,"数据不能为空");
    }






    private CorrelationData SetCallback(String id)
    {
        //        配置ConfirmCallBack
        CorrelationData correlationData=new CorrelationData(id);
        correlationData.getFuture().addCallback(new SuccessCallback<CorrelationData.Confirm>() {
            @Override
            public void onSuccess(CorrelationData.Confirm confirm) {
                if(confirm.isAck())
                    log.info("消息发送成功，id:{}",correlationData.getId());

                else
                    log.error("消息发送到交换机失败，id:{},原因:{}",correlationData.getId(),confirm.getReason());
            }
        }, new FailureCallback() {
            @Override
            public void onFailure(Throwable throwable) {
                log.error("消息发送失败，id:{},愿因:{}",correlationData.getId(),throwable.getMessage());
            }
        });
        return correlationData;
    }
}
